﻿using HookupAppForm.Draw.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace HookupAppForm.Draw
{
    public partial class UC_DrawShow
    {
        private bool isMouseDown = false;
        private Point mouseDownPoint = new Point();
        private double mouseDownImgMoveX = 0;
        private double mouseDownImgMoveY = 0;
        public double ImgLocN { get; set; }
        public double ImgLocS { get; set; }
        public double ImgLocW { get; set; }
        public double ImgLocE { get; set; }
        private Point lastMovePt = new Point();

        private bool strokeGoing = false;

        private double _ImgScaleX = 1;
        public double ImgScaleX
        {
            get { return _ImgScaleX; }
            set
            {
                _ImgScaleX = value;
                cc("ImgScaleX");


                //在滚动停止后，持续静止指定时长后再刷新，否则可能导致刷新频繁 
                DtSclChange = DateTime.Now;
                Task.Run(delegate
                {
                    var waitdu = 200;
                    Thread.Sleep(waitdu);
                    //等待被更新后，延迟小于预期，则不处理
                    var totaluse = (DateTime.Now - DtSclChange).TotalMilliseconds;
                    if (totaluse < waitdu)
                    {
                        return;
                    }

                    cc("EllScaleTimes");
                    cc("EllScaleTimesYFlip");
                    cc("bg_ln_thickness");
                    cc("DesFontSizeWithScale");
                    if (!strokeGoing)
                    {
                        strokeGoing = true;
                        Task.Run(delegate
                        {
                            ResetStroke();
                            strokeGoing = false;
                        });
                    }
                });
            }
        }

        private DateTime DtSclChange = DateTime.MinValue;


        private double _ImgScaleY = 1;
        public double ImgScaleY
        {
            get { return _ImgScaleY; }
            set
            {
                _ImgScaleY = value;
                cc("ImgScaleY");
            }
        }
        private double _ImgMoveX = 0;
        public double ImgMoveX
        {
            get { return _ImgMoveX; }
            set
            {
                _ImgMoveX = value;
                cc("ImgMoveX");
            }
        }
        private double _ImgMoveY = 0;
        public double ImgMoveY
        {
            get { return _ImgMoveY; }
            set
            {
                _ImgMoveY = value;
                cc("ImgMoveY");
            }
        }

        private double ImgWidthInt
        {
            get
            {
                return (double)(cvDrawBg.RenderSize.Width);
            }
        }

        private double ImgHeightInt
        {
            get
            {
                return (double)(cvDrawBg.RenderSize.Height);
            }
        }

        private double _CurrWD = 0;
        public double CurrWD
        {
            get { return _CurrWD; }
            set
            {
                _CurrWD = value;
                cc("CurrWD");
            }
        }

        private double _CurrJD = 0;
        public double CurrJD
        {
            get { return _CurrJD; }
            set
            {
                _CurrJD = value;
                cc("CurrJD");
            }
        }

        public FloorDrawingInfo DrawingCacheInfo { get; internal set; }
        public UC_SysLayout ParentLayout { get; internal set; }

        private void Grid_MouseWheel(object sender, MouseWheelEventArgs e)
        {

            if (CurrDrawCommand != DrawCommand.缩放) return;


            if (imgbg.VerticalAlignment == VerticalAlignment.Center
                && imgbg.HorizontalAlignment == HorizontalAlignment.Center)
            {
                var ePoint = e.GetPosition((sender as UIElement));

                //鼠标位于图片的百分比，在缩放后，仍然处于相同百分比
                var showWidth = ImgWidthInt * ImgScaleX;
                var showHeight = ImgHeightInt * ImgScaleY;
                var ePointXScale = (-ImgMoveX + ePoint.X) / showWidth;
                var ePointYScale = (-ImgMoveY + ePoint.Y) / showHeight;


                //缩放，每次缩放当前图的指定比例
                if (e.Delta > 0)
                {
                    ImgScaleX += ImgScaleX * 0.2;
                    ImgScaleY += ImgScaleY * 0.2;
                }
                else
                {
                    if (ImgScaleX < 0.001) return;

                    ImgScaleX -= ImgScaleX * 0.2;
                    ImgScaleY -= ImgScaleY * 0.2;
                }

                //还原鼠标位置所占大图比例
                showWidth = ImgWidthInt * ImgScaleX;
                showHeight = ImgHeightInt * ImgScaleY;
                ImgMoveX = -(ePointXScale * showWidth - ePoint.X);
                ImgMoveY = -(ePointYScale * showHeight - ePoint.Y);

                //refreshAllTags();
            }
        }

        private void Grid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (CurrDrawCommand != DrawCommand.缩放) return;

            isMouseDown = true;
            mouseDownPoint = e.GetPosition((sender as UIElement));
            mouseDownImgMoveX = ImgMoveX;
            mouseDownImgMoveY = ImgMoveY;
        }
        private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
        {
            isMouseDown = false;
        }

        private void Grid_MouseMove(object sender, MouseEventArgs e)
        {
            if (!isMouseDown || e.LeftButton != MouseButtonState.Pressed) return;
            var currPoint = e.GetPosition((sender as UIElement));
            var xMove = currPoint.X - mouseDownPoint.X;
            var yMove = currPoint.Y - mouseDownPoint.Y;
            ImgMoveX = mouseDownImgMoveX + xMove;
            ImgMoveY = mouseDownImgMoveY + yMove;

            //refreshAllTags();
        }

        private void ImgCnt_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            lastMovePt = new Point(CurrJD, CurrWD);
        }

        /// <summary>
        /// 及时更新位置信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Img_MouseMove(object sender, MouseEventArgs e)
        {

        }

        private void btn_showmapall_click(object sender, RoutedEventArgs e)
        {
            ShowMapAll();
        }




        internal Action<int, int> DrawBgMouseMoveAction;
        internal void ResetBgDrawing()
        {
            if (DrawingCacheInfo != null && DrawingCacheInfo.DrawDetail != null)
            {
                ln_hor_split_arr = DrawingCacheInfo.DrawDetail.ln_hor_split_arr;
                ln_hor_split_arr_sign = DrawingCacheInfo.DrawDetail.ln_hor_split_arr_sign;
                ln_ver_split_arr = DrawingCacheInfo.DrawDetail.ln_ver_split_arr;
                ln_ver_split_arr_sign = DrawingCacheInfo.DrawDetail.ln_ver_split_arr_sign;
            }
            else
            {

                ln_hor_split_arr = "";
                ln_hor_split_arr_sign = "";
                ln_ver_split_arr = "";
                ln_ver_split_arr_sign = "";
            }

            loadBg();


            ShowMapAll();
        }
        internal void ShowMapAll()
        {
            var drawwidth = cvDrawMain.Width;
            var drawheight = cvDrawMain.Height;
            if (drawwidth <= 0) drawwidth = cvdraw_container.ActualWidth;
            if (drawheight <= 0) drawheight = cvdraw_container.ActualHeight;


            var scl_1 = cvdraw_container.ActualWidth / drawwidth;
            var scl_2 = cvdraw_container.ActualHeight / drawheight;
            var scltmp = Math.Min(scl_1, scl_2);

            ImgScaleX = scltmp;
            ImgScaleY = scltmp;

            ImgMoveX = (cvdraw_container.ActualWidth - drawwidth * ImgScaleX) / 2;
            ImgMoveY = (cvdraw_container.ActualHeight - drawheight * ImgScaleY) / 2;
        }
        internal double scaleToInit_X(double x)
        {
            return x / ImgScaleX - ImgMoveX;
        }
        internal double scaleToInit_Y(double x)
        {
            return x / ImgScaleY - ImgMoveY;
        }
    }
}
